const CompressionWebpackPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
    .BundleAnalyzerPlugin
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const HardSourceWebpackPlugin = require("hard-source-webpack-plugin");
const isProduction = process.env.NODE_ENV === 'production'

module.exports = {
    publicPath: process.env.BASE_URL,
    configureWebpack: config => {
        if (isProduction) {
            config.plugins.push(
                new UglifyJsPlugin({
                    uglifyOptions: {
                        warnings: false,
                        compress: {
                            drop_console: true,
                            drop_debugger: false,
                            pure_funcs: ['console.log']
                        }
                    },
                    sourceMap: false,
                    parallel: true
                })
            )
            // 开启gzip压缩
            const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i
            config.plugins.push(
                new CompressionWebpackPlugin({
                    filename: '[path].gz[query]',
                    algorithm: 'gzip',
                    test: productionGzipExtensions,
                    threshold: 10240,
                    minRatio: 0.8
                })
            )
            if (process.env.npm_config_report) {
                // 打包后模块大小分析//npm run build --report
                config.plugins.push(new BundleAnalyzerPlugin())
            }
        } else {
            // 为开发环境修改配置...
            config.plugins.push(
                new HardSourceWebpackPlugin()
            )
        }
    },
    chainWebpack: config => {
        config.plugin('html').tap(args => {
            args[0].VUE_APP_NAME = process.env.VUE_APP_NAME;
            return args;
        });
        config.resolve.alias
            .set('assets', '@/assets')
            .set('components', '@/components')
            .set('views', '@/views')
            .set('style', '@/style')
            .set('api', '@/api')
            .set('http', '@/http')
            .set('store', '@/store')
    },
    css: {
        extract: isProduction ? true:false,
        sourceMap: false,
        requireModuleExtension: false,
        loaderOptions: {
            postcss: { },
            sass: {
                prependData: '@import "style/_mixin.scss";@import "style/_variables.scss";@import "style/common.scss";@import "style/normal.scss";'
            }
        }
    },
    lintOnSave: true, // default false
    // 打包时不生成.map文件
    productionSourceMap: false,
    devServer: {
        compress: true,
        disableHostCheck: true,
        open: true, // 启动服务后是否打开浏览器
        host: '127.0.0.1',
        port: 8088, // 服务端口
        https: false,
        hotOnly: false,
        proxy: 'http://123.56.219.30:8092/integral-mobile'
        // proxy: {
        // '/proxy': {
        //     target: 'https://oapi.dingtalk.com',
        //     changeOrigin: true,
        //     secure: false,
        //     ws: true,
        //     pathRewrite: {
        //         '^/proxy': ''
        //     }
        // },
        // '/integral-mobile': {
        //     target: 'http://123.56.219.30:8092',
        //     changeOrigin: true,
        //     secure: false,
        //     ws: true,
        //     pathRewrite: {
        //         '^/integral-mobile': ''
        //     }
        // }
        // }
    }
}
